package com.eagle.roomsearch.crawler;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author 林良炫
 * @Description: ${todo 网页下载器}
 * @date 2018/4/3  9:47
 */
@Slf4j
public class HttpExecute {

    public final static LinkedBlockingQueue<String> crawlerUrls = new LinkedBlockingQueue<String>(100);//需要抓取的URL队列


    public static void parsePageData(Document document) {
        System.out.println("开始解释" + document);
//        Elements links = document.select("[class=info]").select("h4[class=title]").select("a[href]");
//        System.out.println(links);
//        for(int i = 0 ; i< links.size();i++){
//            HttpExecute.crawlerUrls.offer(links.attr("href"));
//        }
    }

    //1去爬去一次首页
    public static void crawlerHome(String url, Integer timeOut) {
        try {
            Connection.Response response = execute(url, timeOut);
            parsePageData(response.parse());
            System.out.println(crawlerUrls);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        try {
//            String urlBook = crawlerUrls.poll();
//            System.out.println("========================"+crawlerUrls.size());
//            while(urlBook != null){
//                crawlerThread(urlBook,3000);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }


    public static void main(String[] args) {
        try {
            crawlerHome("https://book.douban.com", 3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author llx
     * @Decription 开始爬取线程
     * @date:created in 10:42 2018/4/3
     * @modify by
     */
//    public static void crawlerThread(String url,Integer timeOut) throws Exception {
//        new Thread(() -> {
//            try {
//                boolean flag = true;
//                while(flag){
//                    try {
//                        Connection.Response response = execute(url,timeOut);
//                        DataResolver.parsePageData1(response.parse());
//                    } catch (Exception e1) {
//                        e1.printStackTrace();
//                    }
//                    Thread.sleep(3000);
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }).start();
//    }

    /**
     * 发送http请求
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static Connection.Response execute(String url, Integer timeOut) throws Exception {
        return doExecute(url, timeOut);
    }

    private static Connection.Response doExecute(String url, Integer timeOut) throws Exception {
        Connection connection = Jsoup.connect(url);
        log.info("connection : {}", "" + connection);
        connection.method(Connection.Method.GET).validateTLSCertificates(true).timeout(timeOut)
                .ignoreContentType(true)
                .header("Host", "https://www.baidu.com")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
        Connection.Response response = connection.execute();
        log.info("response : {}", "" + response);
        return response;
    }
}